#include <stdio.h>
#include <version.h>

#if GL_SILENCE_DEPRECATION
#include <GLES2/gl2.h>
#endif

#include <GLFW/glfw3.h>

//反措句柄
static void glfw_error_callback(int error, const char* description)
{
  fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}

//回调函数，用于处理按键事件
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
  //Esc - 关闭窗口
  if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  {
    //设置窗口应该关闭 - Esc
    glfwSetWindowShouldClose(window, GLFW_TRUE);
  }

  //检查是否按下了 - Ctrl + W
  if (key == GLFW_KEY_W && action == GLFW_PRESS && (mods & GLFW_MOD_CONTROL))
  {
    //设置窗口应该关闭
    glfwSetWindowShouldClose(window, GLFW_TRUE);
  }
}

static unsigned char IsWayland()
{
#if !GLFW_HAS_X11_OR_WAYLAND
  return 0;
#elif GLFW_HAS_GETPLATFORM
  return glfwGetPlatform() == GLFW_PLATFORM_WAYLAND;
#else
  const char* version = glfwGetVersionString();
  if (strstr(version, "Wayland") == NULL) // e.g. Ubuntu 22.04 ships with GLFW 3.3.6 compiled without Wayland
    return 0;
#ifdef GLFW_EXPOSE_NATIVE_X11
  if (glfwGetX11Display() != NULL)
    return 0;
#endif
  return 1;
#endif
}

//获取屏幕缩放比例
float getContentScaleForMonitor(GLFWmonitor* monitor)
{
#if GLFW_HAS_X11_OR_WAYLAND
  if (IsWayland()) // We can't access our bd->IsWayland cache for a monitor.
    return 1.0f;
#endif
#if GLFW_HAS_PER_MONITOR_DPI && !(defined(__APPLE__) || defined(__EMSCRIPTEN__) || defined(__ANDROID__))
  float x_scale, y_scale;
  glfwGetMonitorContentScale(monitor, &x_scale, &y_scale);
  return x_scale;
#else
  UNUSED(monitor);
  return 1.0f;
#endif
}

int main(int argc,char **argv)
{
  int screenWidth;
  int screenHeight;
  int windowWidth;
  int windowHeight;
  int windowX;
  int windowY;
  int displayW;
  int displayH;

  const char* glsl_version = "#version 130";
  const GLFWvidmode* videoMode = NULL;
  GLFWmonitor* monitor = NULL;
  GLFWwindow* window = NULL;

  //设置窗口大小
  windowWidth = 800;
  windowHeight = 600;

  glfwSetErrorCallback(glfw_error_callback);

  //初始化GLFW
  if (!glfwInit())
  {
    return -1;
  }

  //设置OpenGL上下文的主要版本号为3
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);

  //设置OpenGL上下文的次要版本号为0
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

  //设置客户端API为OpenGL
  glfwWindowHint(GLFW_CLIENT_API,GLFW_OPENGL_API);

  //获取默认显示器
  monitor = glfwGetPrimaryMonitor();

  //获取视频模式
  videoMode = glfwGetVideoMode(monitor);
  screenWidth = videoMode->width;
  screenHeight = videoMode->height;

  //计算窗口居中的位置
  windowX = (screenWidth - windowWidth) / 2;
  windowY = (screenHeight - windowHeight) / 2;

  //获取缩放比例
  float scale = getContentScaleForMonitor(monitor);

  //创建窗口
  window = glfwCreateWindow(windowWidth * scale, windowHeight * scale, "OpenGL Window", NULL, NULL);
  if (window == NULL)
  {
    fprintf(stderr, "Failed to create GLFW window.\n");
    glfwTerminate();
    return -1;
  }

  //设置窗口位置
  glfwSetWindowPos(window, windowX, windowY);

  //设置上下文
  glfwMakeContextCurrent(window);

  //设置按键回调函数
  glfwSetKeyCallback(window, key_callback);

  //启用垂直同步
  glfwSwapInterval(1);

  //开始 - 添加用户代码

  //结束 - 添加用户代码

  //主循环
  while (!glfwWindowShouldClose(window))
  {
    //处理事件
    glfwPollEvents();

    //检查窗口是否被最小化
    if (glfwGetWindowAttrib(window,GLFW_ICONIFIED) != 0)
    {
      usleep(10 * 1000);
      continue;
    }

    //开始 - 写自定义代码

    //结束 - 写自定义代码

    //获取帧缓冲区大小
    glfwGetFramebufferSize(window,&displayW,&displayH);

    //设置视口
    glViewport(0,0,displayW,displayH);

    //清除颜色缓冲区
    //GL_DEPTH_BUFFER_BIT   - 清除深度缓冲区
    //GL_STENCIL_BUFFER_BIT - 清除模板缓冲区
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //交换前后缓冲区
    glfwSwapBuffers(window);
  }

  //销毁窗口并终止GLFW
  glfwDestroyWindow(window);
  glfwTerminate();

  UNUSED(argc);
  UNUSED(argv);
  return 0;
}
